<template>
  <div class="main" style="position: relative">
    <!-- 卡片视图  -->
    <el-card>
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator="/" style="margin-bottom: 10px">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">权限管理</a></el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 添加角色按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addDialogVisible = true"
            >添加角色</el-button
          >
        </el-col>
      </el-row>

      <!-- 角色列表区域 -->
      <el-table :data="rolesList" border stripe>
        <!-- 索引 -->
        <el-table-column type="index" label="#" width="180"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="180">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" width="180">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="showEditDialog(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="removeFn(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加角色  -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="roleName"
        >
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="formLabelWidth"
          prop="roleDesc"
        >
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 按钮底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFn">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改角色 -->
    <el-dialog title="修改角色" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="120px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="putItem">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  RolesAPI,
  postRolesAPI,
  putRolesAPI,
  deleteRolesAPI,
} from "@/api/index";
export default {
  data() {
    return {
      rolesList: [],
      // 添加角色
      addDialogVisible: false,
      formLabelWidth: "120px",
      addForm: {
        roleName: "",
        roleDesc: "",
      },

      addFormRules: {
        roleName: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 6,
            message: "名称长度在2-6个字之间",
            trigger: "blur",
          },
        ],
        roleDesc: [
          {
            required: true,
            message: "请输入角色描述",
            trigger: "blur",
          },
          {
            min: 2,
            max: 15,
            message: "描述长度在2-15个字之间",
            trigger: "blur",
          },
        ],
      },
      // 修改角色
      editForm: {
        roleName: "",
        roleDesc: "",
      },
      editDialogVisible: false,
      editFormRules: {
        roleName: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 6,
            message: "名称长度在2-6个字之间",
            trigger: "blur",
          },
        ],
        roleDesc: [
          {
            required: true,
            message: "请输入角色描述",
            trigger: "blur",
          },
          {
            min: 2,
            max: 15,
            message: "描述长度在2-15个字之间",
            trigger: "blur",
          },
        ],
      },
    };
  },

  // async created() {
  //   const res = await RolesAPI();
  //   console.log(res);
  // },
  created() {
    this.getRolesList();
  },
  methods: {
    async getRolesList() {
      const res = await RolesAPI();
      if (res.data.meta.status !== 200) {
        return;
      }
      // console.log(res);
      this.rolesList = res.data.data;
    },
    // 对话框关闭 重置表单
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    // 点击确定，添加内容
    submitFn() {
      this.$refs.addFormRef.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        // 发起请求，添加角色
        const res = await postRolesAPI(this.addForm);
        console.log("--------------");
        console.log(res);
        if (res.data.meta.status !== 200) {
          this.$message.error("添加失败！");
        }
        this.$message.success("信息更新成功！");
        this.addDialogVisible = false;
        this.getRolesList();
      });
    },
    // 显示编辑对话框
    async showEditDialog(item) {
      this.editDialogVisible = true;
      console.log(item);
      this.editForm = item;
    },
    async putItem() {
      console.log(this.editForm);
      // 关闭编辑弹窗
      this.editDialogVisible = false;
      const res = await putRolesAPI(this.editForm.id, this.editForm);
      console.log(res);
      this.getRolesList();
    },
    //  删除
    async removeFn(id) {
      // 对应的id
      console.log(id);
      // 询问
      const confirmResult = await this.$confirm(
        "此操作将永久删除该角色, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const res = await deleteRolesAPI(id);
      if (res.data.meta.status !== 200) {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除用户成功");
      this.getRolesList();
    },
  },
};
</script>


<style scoped>
</style>